﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OPLWeb.Models;

namespace OPLWeb.Controllers
{
    public class FixturesController : Controller
    {
        //
        // GET: /Fixture/
        OPLWeb.Models.ORITAEntities db = new Models.ORITAEntities();

        public ActionResult Index(int Id)
        {
            FixtureWrapper wrapper = new FixtureWrapper();
            ViewBag.CurrentSeason = Id;

            wrapper.FullFixture = db.Fixtures.Where(p => p.SeasonId == Id).ToList();
            wrapper.NextGame = wrapper.FullFixture.Where(p => p.Date > DateTime.Now).FirstOrDefault();

            DateTime firstDay = Convert.ToDateTime("2012-01-01 00:00:00.000");
            wrapper.GroupedFixture =
                (from t in db.Fixtures.AsEnumerable()
                 group t by new { WeekNumber = (t.Date - firstDay).Days / 7 } into ut
                 select new GroupedFixture
                 {
                     Fixtures = ut.ToList(),
                     WeekNumber = ut.Key.WeekNumber
                 }).ToList();

            return View(wrapper);
        }

        //
        // GET: /Fixture/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Fixture/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Fixture/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Fixture/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Fixture/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Fixture/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Fixture/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
